﻿        function newFile(o,UCName)//可选多个附件的控件使用
        {   
            var maxId = document.getElementById(UCName+'_hidMaxId').value;  
            if (!isSameRecord(UCName,o.value,'more'))
	        {
	             maxId++;
	             id=maxId;
	             var p=o.parentElement; 
	             p.insertAdjacentHTML("beforeEnd",fileTxt(id));
	            return;
	        }
	        maxId++;
	        id=maxId;
	        document.getElementById(UCName+'_hidMaxId').value = maxId;
	        var p=o.parentElement; 
	         p.insertAdjacentHTML("beforeEnd",fileTxt(id));
	        
	        if (document.getElementById(UCName+'_Hdn_Type').value!="")
	        {
	           document.getElementById(UCName+'_Hdn_Type').value=document.getElementById(UCName+'_Hdn_Type').value+',0';
	           document.getElementById(UCName+'_Hdn_Name').value=document.getElementById(UCName+'_Hdn_Name').value+','+GetFileName(o.value);
	           document.getElementById(UCName+'_Hdn_Luj').value=document.getElementById(UCName+'_Hdn_Luj').value+','+GetLocaladd(o.value);	
	           document.getElementById(UCName+'_BtnSaveFile').click();         
	        }
	        else
	        {
	           document.getElementById(UCName+'_Hdn_Type').value='0';
	           document.getElementById(UCName+'_Hdn_Name').value=GetFileName(o.value);
	           document.getElementById(UCName+'_Hdn_Luj').value=GetLocaladd(o.value);	
	           document.getElementById(UCName+'_BtnSaveFile').click();        
	        }
	      //  SetTabelDivOne();        
         }    
       function newOneFile(o,UCName) //只可选单个附件使用
        { 
            var maxId = document.getElementById(UCName+'_hidMaxId').value;  
            if (!isSameRecord(UCName,o.value,'one'))
	        {
	             maxId++;
	             id=maxId;
	             var p=o.parentElement; 
	             p.insertAdjacentHTML("beforeEnd",onefileTxt(id));
	            return;
	        }
	        maxId++;
	        id=maxId;
	        document.getElementById(UCName+'_hidMaxId').value = maxId;
	        var p=o.parentElement;
	        p.insertAdjacentHTML("beforeEnd",onefileTxt(id));
	        document.getElementById(UCName+'_Hdn_Name').value= GetFileName(o.value); 
	        if (document.getElementById(UCName+'_Hdn_Type').value!="")
	        {
	           var reg=new RegExp("0","g"); //创建正则RegExp对象
               var stringObj=document.getElementById(UCName+'_Hdn_Type').value;
               var newstr=stringObj.replace(reg,"-1"); 
	       
	           document.getElementById(UCName+'_Hdn_Type').value=newstr+',0';
	           document.getElementById(UCName+'_Hdn_Luj').value=document.getElementById(UCName+'_Hdn_Luj').value+','+GetLocaladd(o.value);	
	           document.getElementById(UCName+'_BtnSaveFile').click();         
	        }
	        else
	        {
	           document.getElementById(UCName+'_Hdn_Type').value='0';
	           document.getElementById(UCName+'_Hdn_Luj').value=GetLocaladd(o.value);	
	           document.getElementById(UCName+'_BtnSaveFile').click();        
	        }       
         }    
         ///判断是否有重复记录
         function isSameRecord(UCName,s,flag)
         {
             var aa=document.getElementById(UCName+'_Hdn_Type').value.split(",");
             var ss=document.getElementById(UCName+'_Hdn_Name').value.split(",");
             var jj="";
             for (var i=0;i<aa.length;i++)
             {
                 if (aa[i]!="-1" && aa[i]!="")
                 {                      
                     if (ss[i]==ForMatName(s))
                     {
                        if (confirm('存在相同文件，是否替换？'))     
                        {               
                           delFile(UCName,flag,i+1);    
                        }     
                        else
                        {
                           jj="1";
                           break;                           
                        }
                     } 
                 }             
             }       
             if (jj!="") 
                 return false;
             else
                 return true;               
         }
         
         //删除附件
         //将type改为-1；
         function delFile(UCName,flag,id)
         {   
            var aa=document.getElementById(UCName.toString()+'_Hdn_Type').value.split(",");
            var a_value="";       
            if (aa.length>=1)
            {
                for (var i=0;i<aa.length;i++)
                {
                   if (i!=(parseInt(id)-1))
                   {
                       if (a_value=="")
                       {
                          a_value+=aa[i];                                    ;
                       }
                       else
                       {
                          a_value=a_value+","+aa[i];        
                       }                   
                   }
                   else
                      if (a_value=="")
                       {
                          a_value+="-1";                                         
                       }
                       else
                       {
                          a_value=a_value+",-1";                        
                       }    
                }                                   
            }        
            document.getElementById(UCName+'_Hdn_Type').value=a_value;     
            SetTableDiv(UCName,flag);
         }
         
         
         function fileTxt(id)
         {
            var ret;
            ret='<input id="tfile" class="file1" onChange="newFile(this)" type="file" style="z-index: 100; width: 58px; top: 3px; height: 16px; left: 58px;" size="1" runat="server"> ';
            ret=ret.replace(/tfile/g,"tfile"+id);
           	return ret;
         }
	      function onefileTxt(id)
         {
            var ret;
            ret='<input id="tfile" class="file1" onChange="newOneFile(this)" type="file" style="z-index: 100; width: 58px; top: 3px; height: 16px; left: 58px;" size="1" runat="server"> ';
            ret=ret.replace(/tfile/g,"tfile"+id);
           	return ret;
         }
	     //增加删除图标及超链
         function SetdelText(id,UCName,flag)
         {
            if (document.getElementById(UCName+'_PageVisble').value=="0")
            {
                  return '<a href="javascript:void(0)" onclick="delFile(\''+UCName+'\',\''+flag+'\','+id+')"><span class="STYLE4">×</span> 移除</a>';
            }
            else
            {      
               document.getElementById(UCName+'_upfile').style.display="none";
                return "";
            }
         }
         
         //删除所有记录
         
         function DeleteRowTable(strTableId)
         {
             var objTable = document.getElementById(strTableId);
             if (objTable == null) 
             {
                return;
             }           
             var cc="";  
          //   alert("tabke"+objTable.rows.length)
             var j=objTable.rows.length;
             for (var i = j-1; i > 0; i--) 
             {   
                objTable.deleteRow(i);
             }   
         }
         
         function GetQryRes(UCName)
         {
              var aa=document.getElementById(UCName+'_Hdn_Type').value.split(",");
              var i=0;
              for (var j=0;j<aa.length;j++)
              {
                 if (aa[i]=="-1")
                     i+=1;                 
              } 
              return i;
         }
         
         ///添加文件到table
         function SetTableDiv(UCName,flag)
         {                                   
             var aa=document.getElementById(UCName+'_Hdn_Type').value.split(",");
             var bb=document.getElementById(UCName+'_Hdn_Luj').value.split(",");               
             var ee="";
              if(flag=='more') 
              ee=document.getElementById(UCName+'_Hdn_Name').value.split(",");
              else
              ee=document.getElementById(UCName+'_Hdn_Name').value;
             var cc="";
             var jj=0;
             var dd=document.getElementById(UCName+'_Hdn_Url').value.split(",");               
             var ss =dd.length-aa.length;
             var ii=aa.length-GetQryRes(UCName);           
             DeleteRowTable(UCName+'_fjtable');            
             if (ii>2)
             {                 
                 var ii=Math.ceil((ii-2)/3);                 
                 for (var j=0;j<ii;j++)
                 {
                    var index=(j+1)*3;                              
                    addRowToTable(UCName+'_fjtable', new Array(UCName+'_files'+parseInt((j+1)*3), UCName+'_files'+parseInt((j+1)*3+1),UCName+'_files'+parseInt((j+1)*3+2)));  
                 }             
             }  
             document.getElementById(UCName+'_files1').innerHTML = ""; 
             document.getElementById(UCName+'_files2').innerHTML = "";                   
         
             if (aa!="")
             {
                for(var i=1;i<=aa.length;i++)
                {         
                  if (aa[i-1]!="-1")   
                  {    
                     jj+=1;
                     cc=UCName+"_files"+jj;  
                     if(flag=='more')                    
                       document.getElementById(cc).innerHTML = "<div id='fn"+i+"'>"+ForFileName(dd[i+ss-1],ee[i-1])+""+SetdelText(i,UCName,'more')+"</div>";
                     else
                     {
                       document.getElementById(cc).innerHTML = "<div id='fn"+i+"'>"+ForFileName(dd[i-1],ee)+""+SetdelText(i,UCName,'one')+"</div>";
                       break;
                     }
                   }
                } 
             }           
         }

         function ForFileName(s,y)
         {
	         var Max=300,l,r,t,o=s;
	        
	         t=s.split(".");
	         t=t[t.length-1];
	         if (s.length>Max)
	         {
		        r=s.split("\\");
		        r=Len(r[r.length-1])+1;
		        l=Max-r-5;
		        s=Left(s,l)+"....."+Right(s,r);
	         } //_blank	        
	          
	         t='<img border=0 src="'+SetfilePic(t)+'" width=16 height=16> ';
	         return '<a href="'+o+'" target="_blank">'+t+y+'&nbsp;&nbsp;</a>';
         }
         
         function ForMatName(s)
         {
             var t,r;            
             t=s.lastIndexOf("\\");
             r=s.substr(t+1,s.length-t);
             //alert(r);
             return r;
         }
         
         //获得文件名
         function GetFileName(s)
         {
             var Max=300,l,r,t,o=s;	         
	         if (s.length>Max)
	         {
	         
		        r=s.split("\\");		   
		        r=Len(r[r.length-1])+1;
		        l=Max-r-5;
		        s=Left(s,l)+"....."+Right(s,r);
		        return r[r.length-1];
	         }
	         return ForMatName(s);
         }
         
         function GetLocaladd(s)
         {
             var Max=300,l,r,t,o=s;	         
	         if (s.length>Max)
	         {
		        r=s.split("\\");
		        r=Len(r[r.length-1])+1;		       
		        l=Max-r-5;
		        s=Left(s,l)+"....."+Right(s,r);
	         }
	         return s;         
         }
         
         ///获取文件类型
         function GetFileType(filename)
         {
             var t,r;            
             t=filename.lastIndexOf(".");
             r=s.substr(t+1,s.length-t);
             return r;
         }
         
         //获得对应文件图片
         function SetfilePic(t)
         {
	        filepath="../images/fileupload/";
	        var hz=t.split("&");
	        switch (hz[0].toLowerCase())
	        {
	           case "doc" : t="msword.gif";break;
	           case "xls" : t="msexcel.gif";break;
	           case "jpg" : t="image.gif";break;
	           case "gif" : t="image.gif";break;
	           case "bmp" : t="image.gif";break;
	           case "png" : t="image.gif";break;
	           case "rar" : t="archive.gif";break;
	           case "zip" : t="archive.gif";break;
	           case "htm" : t="htm.gif";break;
	           case "html" : t="htm.gif";break;
	           case "js" : t="htm.gif";break;
	           case "asp" : t="htm.gif";break;
	           case "aspx" : t="htm.gif";break;
	           case "xml" : t="htm.gif";break;
	           default : t="unknown.gif";
	        }	     
	        return filepath+t;
        }   
        
//        
//        ///以下暂不使用
//        ///单个增加,暂不使用
//         
//         function SetTabelDivOne()
//         {            
//             var aa=document.getElementById('<%=Hdn_Type.ClientID%>').value.split(",");
//             var bb=document.getElementById('<%=Hdn_Luj.ClientID%>').value.split(",");     
//             var ee=document.getElementById('<%=Hdn_Name.ClientID%>').value.split(",");   
//                 
//             var cc="files1";  
//             var dd=document.getElementById('<%=Hdn_Url.ClientID%>').value.split(",");  
//             if (aa.length>2)
//             {             
//                 var ii=(aa.length-2)%3;   
//                 alert("ii:"+ii);             
//                 if (ii==1)
//                 {
//                   j=Math.ceil((aa.length-2)/3);
//                   var index=(j+1)*3;  
//                   alert(index);               
//                   addRowToTable('fjtable', new Array('files'+parseInt(j*3), 'files'+parseInt(j*3+1),'files'+parseInt(j*3+2))); 
//                 } 
//                        
//             }                   
//   
//             cc="files"+aa.length;                  
//             document.getElementById(cc).innerHTML += "<div id='fn"+aa.length+"'>"+ForFileName(dd[aa.length-1],ee[aa.length-1])+"&nbsp;&nbsp;"+SetdelText(aa.length,UCName)+"</div>";
//      
//         }
         
         function addRowToTable(strTableId, arrTdText)
         {
             var objTable = document.getElementById(strTableId);
             if (objTable == null) 
             {
                return;
             }
             var objTableBody = objTable.getElementsByTagName("TBODY")[0];
             var row = document.createElement("TR")
             for (var i = 0; i < arrTdText.length; i++) 
             {
                var col = document.createElement("TD")
                col.id=arrTdText[i];
              //  col.appendChild(document.createTextNode(arrTdText[i]))
               
                row.appendChild(col);
             }
             objTableBody.appendChild(row);
         }